﻿using CarPCBATest.Procedure;
using Common;
using Device;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace CarPCBATest.ViewModel
{
    internal class DebugViewModel : NotifyBase
    {
        bool loaded = false;

        private bool _ioAir;

        /// <summary>
        /// 气缸下压
        /// </summary>
        public bool IOAir
        {
            get { return _ioAir; }
            set
            {
                _ioAir = value;
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.AirReset, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioOKLed;

        /// <summary>
        /// 绿灯
        /// </summary>
        public bool IOOkLed
        {
            get { return _ioOKLed; }
            set
            {
                _ioOKLed = value;
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.GreenLed, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioYellowLed;
        /// <summary>
        /// 黄灯
        /// </summary>
        public bool IOYellowLed
        {
            get { return _ioYellowLed; }
            set
            {
                _ioYellowLed = value;
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.YellowLed, value ? 1 : 0);
                this.DoNotify();
            }
        }


        private bool _ioNgLed;

        /// <summary>
        /// 报警灯
        /// </summary>
        public bool IONgLed
        {
            get { return _ioNgLed; }
            set
            {
                _ioNgLed = value;
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.RedLed, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioPower1;

        public bool IOPower1
        {
            get { return _ioPower1; }
            set
            { 
                _ioPower1 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO1, value ? 1 : 0);
            }
        }

        private bool _ioPower2;

        public bool IOPower2
        {
            get { return _ioPower2; }
            set
            {
                _ioPower2 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO2, value ? 1 : 0);
            }
        }

        private bool _ioPower3;

        public bool IOPower3
        {
            get { return _ioPower3; }
            set
            {
                _ioPower3 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO3, value ? 1 : 0);
            }
        }

        private bool _ioPower4;

        public bool IOPower4
        {
            get { return _ioPower4; }
            set
            {
                _ioPower4 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO4, value ? 1 : 0);
            }
        }

        private bool _ioPower5;

        public bool IOPower5
        {
            get { return _ioPower5; }
            set
            {
                _ioPower5 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO5, value ? 1 : 0);
            }
        }
        private bool _ioPwer6;

        public bool IOPower6
        {
            get { return _ioPwer6; }
            set
            {
                _ioPwer6 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO6, value ? 1 : 0);
            }
        }

        private bool _ioPower7;

        public bool IOPower7
        {
            get { return _ioPower7; }
            set
            {
                _ioPower7 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO7, value ? 1 : 0);
            }
        }
        private bool _ioPower8;

        public bool IOPower8
        {
            get { return _ioPower8; }
            set
            {
                _ioPower8 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO8, value ? 1 : 0);
            }
        }

        private bool _ioPower9;

        public bool IOPower9
        {
            get { return _ioPower9; }
            set
            {
                _ioPower9 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO9, value ? 1 : 0);
            }
        }
        private bool _ioPower10;

        public bool IOPower10
        {
            get { return _ioPower10; }
            set
            {
                _ioPower10 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO10, value ? 1 : 0);
            }
        }

        private bool _ioPower11;

        public bool IOPower11
        {
            get { return _ioPower11; }
            set
            {
                _ioPower11 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO11, value ? 1 : 0);
            }
        }

        private bool _ioPower12;

        public bool IOPower12
        {
            get { return _ioPower12; }
            set
            {
                _ioPower12 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO12, value ? 1 : 0);
            }
        }
        private bool _ioPower13;

        public bool IOPower13
        {
            get { return _ioPower13; }
            set
            {
                _ioPower13 = value;
                this.DoNotify();
                if (!isRead)
                    IOModuleDO.SetDOValue(DODefine.DO13, value ? 1 : 0);
            }
        }




        private bool _ioSafe;

        /// <summary>
        /// 安全光幕
        /// </summary>
        public bool IOSafe
        {
            get { return _ioSafe; }
            set
            {
                _ioSafe = value;
                this.DoNotify();
            }
        }

        private bool _ioStartButton;

        /// <summary>
        /// 启动按钮
        /// </summary>
        public bool IOStartButton
        {
            get { return _ioStartButton; }
            set
            {
                _ioStartButton = value;
                this.DoNotify();
            }
        }

        private bool _ioEmgStop;

        /// <summary>
        /// 急停按钮
        /// </summary>
        public bool IOEmgStop
        {
            get { return _ioEmgStop; }
            set
            {
                _ioEmgStop = value;
                this.DoNotify();
            }
        }

        private bool _ioResetButtton;

        /// <summary>
        /// 复位按钮
        /// </summary>
        public bool IOResetButton
        {
            get { return _ioResetButtton; }
            set
            {
                _ioResetButtton = value;
                this.DoNotify();
            }
        }

        private bool _ioAirWorkSenser;

        /// <summary>
        /// 气缸到位
        /// </summary>
        public bool IOAirWorkSenser
        {
            get { return _ioAirWorkSenser; }
            set
            {
                _ioAirWorkSenser = value;
                this.DoNotify();
            }
        }

        private string _current;

        /// <summary>
        /// 电流表电流
        /// </summary>
        public string Current
        {
            get { return _current; }
            set
            {
                _current = value;
                this.DoNotify();
            }
        }

        private double _power;

        /// <summary>
        /// 电压表设置电压
        /// </summary>
        public double Power
        {
            get { return _power; }
            set
            {
                _power = value;
                this.DoNotify();
            }
        }

        private int _selectedChannel;

        /// <summary>
        /// 电压表通道
        /// </summary>
        public int SelectedChannel
        {
            get { return _selectedChannel; }
            set
            {
                _selectedChannel = value;
                this.DoNotify();
            }
        }

        private double _current2;

        /// <summary>
        /// 电压表电流
        /// </summary>
        public double Current2
        {
            get { return _current2; }
            set
            {
                _current2 = value;
                this.DoNotify();
            }
        }

        public CommandBase GetCurrentCommand { get; set; }
        public CommandBase SetPowerCommand { get; set; }
        public CommandBase SetCurrentCommand { get; set; }
        public CommandBase UnloadedCommand { get; set; }
        public CommandBase LoadedCommand { get; set; }
        public CommandBase SetRemoteCommand { get; set; }
        public CommandBase OnChannelCommand { get; set; }
        public CommandBase OffChannelCommand { get; set; }

        public DebugViewModel()
        {
            try
            {
                SelectedChannel = 1;
                GetCurrentCommand = new CommandBase(GetCurrent);
                SetPowerCommand = new CommandBase(SetPower);
                SetCurrentCommand = new CommandBase(SetCurrent);
                UnloadedCommand = new CommandBase(Unloaded);
                LoadedCommand = new CommandBase(Loaded);
                SetRemoteCommand = new CommandBase(SetRemote);
                OnChannelCommand = new CommandBase(OnChannel);
                OffChannelCommand = new CommandBase(OffChannel);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OffChannel(object obj)
        {
            try
            {
                IV_3003T.SwitchCh((IV3003T_CH_ENUM)SelectedChannel);
                IV_3003T.SetChStatus(0);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OnChannel(object obj)
        {
            try
            {
                IV_3003T.SwitchCh((IV3003T_CH_ENUM)SelectedChannel);
                IV_3003T.SetChStatus(1);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetRemote(object obj)
        {
            try
            {
                IV_3003T.SetRemote();
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        bool isRead;

        private void Loaded(object obj)
        {
            try
            {
                DM3058E.Connect(PcbConfigHelper.AllPcbConfig.DM3025EPort);
                IV_3003T.Connect(PcbConfigHelper.AllPcbConfig.IV3003TPort);
                IV_3003T.Init();

                IOModule.Connect();
                Thread.Sleep(100);
                loaded = true;
                Task.Run(() =>
                {
                    while (loaded)
                    {
                        isRead = true;
                        IOStartButton = IOModule.GetDIValue(DIDefine.Start);
                        IOResetButton = IOModule.GetDIValue(DIDefine.Reset);
                        IOSafe = IOModule.GetDIValue(DIDefine.SafeLight);
                        IOAirWorkSenser = IOModule.GetDIValue(DIDefine.AirWork);
                        IOEmgStop = IOModule.GetDIValue(DIDefine.EmgStop);

                        IOAir = IOModuleDO.GetDOValue(DODefine.AirReset);
                        IOOkLed = IOModuleDO.GetDOValue(DODefine.GreenLed);
                        IOYellowLed = IOModuleDO.GetDOValue(DODefine.YellowLed);
                        IONgLed = IOModuleDO.GetDOValue(DODefine.RedLed);
                        IOPower1 = IOModuleDO.GetDOValue(DODefine.DO1);
                        IOPower2 = IOModuleDO.GetDOValue(DODefine.DO2);
                        IOPower3 = IOModuleDO.GetDOValue(DODefine.DO3);
                        IOPower4 = IOModuleDO.GetDOValue(DODefine.DO4);
                        IOPower5 = IOModuleDO.GetDOValue(DODefine.DO5);
                        IOPower6 = IOModuleDO.GetDOValue(DODefine.DO6);
                        IOPower7 = IOModuleDO.GetDOValue(DODefine.DO7);
                        IOPower8 = IOModuleDO.GetDOValue(DODefine.DO8);
                        IOPower9 = IOModuleDO.GetDOValue(DODefine.DO9);
                        IOPower10 = IOModuleDO.GetDOValue(DODefine.DO10);
                        IOPower11 = IOModuleDO.GetDOValue(DODefine.DO11);
                        IOPower12 = IOModuleDO.GetDOValue(DODefine.DO12);
                        IOPower13 = IOModuleDO.GetDOValue(DODefine.DO13);
                        isRead = false;
                        Thread.Sleep(500);
                    }
                });
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void Unloaded(object obj)
        {
            loaded = false;
        }

        private void SetCurrent(object obj)
        {
            try
            {
                //IT6302A.SetCurrent(SelectedChannel, Current2);
                IV_3003T.SwitchCh((IV3003T_CH_ENUM)SelectedChannel);
                IV_3003T.SetCurrent(Current2);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetPower(object obj)
        {
            try
            {
                //IT6302A.SetVoltage(SelectedChannel, Power);
                IV_3003T.SwitchCh((IV3003T_CH_ENUM)SelectedChannel);
                IV_3003T.SetVoltage(Power);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void GetCurrent(object obj)
        {
            try
            {
                var tmp = DM3058E.GetValue(Function.DCI);
                if (Math.Abs(tmp) > 1)
                    Current = tmp + "A";
                else if (Math.Abs(tmp * 1000) > 1)
                    Current = tmp * 1000 + "mA";
                else if (Math.Abs(tmp * 1000000) > 1)
                    Current = tmp * 1000000 + "μA";
                else
                {
                    Current = tmp + "A";
                }
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }
    }
}